/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package kimo.persistence.controllers;

import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import kimo.persistence.controllers.exceptions.NonexistentEntityException;
import kimo.persistence.entities.RawProduct;
import kimo.persistence.entities.Product;
import kimo.persistence.entities.Producer;
import kimo.persistence.entities.Production;

/**
 *
 * @author a381u296
 */
public class ProductionJpaController implements Serializable {

    public ProductionJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Production production) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            RawProduct productionRawProductId = production.getProductionRawProductId();
            if (productionRawProductId != null) {
                productionRawProductId = em.getReference(productionRawProductId.getClass(), productionRawProductId.getRawProductId());
                production.setProductionRawProductId(productionRawProductId);
            }
            Product productionProductId = production.getProductionProductId();
            if (productionProductId != null) {
                productionProductId = em.getReference(productionProductId.getClass(), productionProductId.getProductId());
                production.setProductionProductId(productionProductId);
            }
            Producer productionProducerId = production.getProductionProducerId();
            if (productionProducerId != null) {
                productionProducerId = em.getReference(productionProducerId.getClass(), productionProducerId.getProducerId());
                production.setProductionProducerId(productionProducerId);
            }
            em.persist(production);
            if (productionRawProductId != null) {
                productionRawProductId.getProductionList().add(production);
                productionRawProductId = em.merge(productionRawProductId);
            }
            if (productionProductId != null) {
                productionProductId.getProductionList().add(production);
                productionProductId = em.merge(productionProductId);
            }
            if (productionProducerId != null) {
                productionProducerId.getProductionList().add(production);
                productionProducerId = em.merge(productionProducerId);
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Production production) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Production persistentProduction = em.find(Production.class, production.getProductionId());
            RawProduct productionRawProductIdOld = persistentProduction.getProductionRawProductId();
            RawProduct productionRawProductIdNew = production.getProductionRawProductId();
            Product productionProductIdOld = persistentProduction.getProductionProductId();
            Product productionProductIdNew = production.getProductionProductId();
            Producer productionProducerIdOld = persistentProduction.getProductionProducerId();
            Producer productionProducerIdNew = production.getProductionProducerId();
            if (productionRawProductIdNew != null) {
                productionRawProductIdNew = em.getReference(productionRawProductIdNew.getClass(), productionRawProductIdNew.getRawProductId());
                production.setProductionRawProductId(productionRawProductIdNew);
            }
            if (productionProductIdNew != null) {
                productionProductIdNew = em.getReference(productionProductIdNew.getClass(), productionProductIdNew.getProductId());
                production.setProductionProductId(productionProductIdNew);
            }
            if (productionProducerIdNew != null) {
                productionProducerIdNew = em.getReference(productionProducerIdNew.getClass(), productionProducerIdNew.getProducerId());
                production.setProductionProducerId(productionProducerIdNew);
            }
            production = em.merge(production);
            if (productionRawProductIdOld != null && !productionRawProductIdOld.equals(productionRawProductIdNew)) {
                productionRawProductIdOld.getProductionList().remove(production);
                productionRawProductIdOld = em.merge(productionRawProductIdOld);
            }
            if (productionRawProductIdNew != null && !productionRawProductIdNew.equals(productionRawProductIdOld)) {
                productionRawProductIdNew.getProductionList().add(production);
                productionRawProductIdNew = em.merge(productionRawProductIdNew);
            }
            if (productionProductIdOld != null && !productionProductIdOld.equals(productionProductIdNew)) {
                productionProductIdOld.getProductionList().remove(production);
                productionProductIdOld = em.merge(productionProductIdOld);
            }
            if (productionProductIdNew != null && !productionProductIdNew.equals(productionProductIdOld)) {
                productionProductIdNew.getProductionList().add(production);
                productionProductIdNew = em.merge(productionProductIdNew);
            }
            if (productionProducerIdOld != null && !productionProducerIdOld.equals(productionProducerIdNew)) {
                productionProducerIdOld.getProductionList().remove(production);
                productionProducerIdOld = em.merge(productionProducerIdOld);
            }
            if (productionProducerIdNew != null && !productionProducerIdNew.equals(productionProducerIdOld)) {
                productionProducerIdNew.getProductionList().add(production);
                productionProducerIdNew = em.merge(productionProducerIdNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = production.getProductionId();
                if (findProduction(id) == null) {
                    throw new NonexistentEntityException("The production with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Production production;
            try {
                production = em.getReference(Production.class, id);
                production.getProductionId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The production with id " + id + " no longer exists.", enfe);
            }
            RawProduct productionRawProductId = production.getProductionRawProductId();
            if (productionRawProductId != null) {
                productionRawProductId.getProductionList().remove(production);
                productionRawProductId = em.merge(productionRawProductId);
            }
            Product productionProductId = production.getProductionProductId();
            if (productionProductId != null) {
                productionProductId.getProductionList().remove(production);
                productionProductId = em.merge(productionProductId);
            }
            Producer productionProducerId = production.getProductionProducerId();
            if (productionProducerId != null) {
                productionProducerId.getProductionList().remove(production);
                productionProducerId = em.merge(productionProducerId);
            }
            em.remove(production);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Production> findProductionEntities() {
        return findProductionEntities(true, -1, -1);
    }

    public List<Production> findProductionEntities(int maxResults, int firstResult) {
        return findProductionEntities(false, maxResults, firstResult);
    }

    private List<Production> findProductionEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Production.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Production findProduction(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Production.class, id);
        } finally {
            em.close();
        }
    }

    public int getProductionCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Production> rt = cq.from(Production.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
